package org.opens.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.opens.dto.BaseUserPageDTO;
import org.opens.entity.BaseUser;
import org.opens.service.BaseUserService;
import org.opens.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/baseUser")
public class BaseUserController {

    @Autowired
    private BaseUserService baseUserService;

    @RequestMapping(value = "/insert")
    public R<BaseUser> save(@RequestBody BaseUser baseUser) {
        return R.success(baseUserService.saveOrUpdateEntityInfo(baseUser));
    }

    /**
     * 测试mybatis plus的updateById方法
     * @param baseUser  更新信息
     */
    @RequestMapping(value = "/updateById")
    public R<String> updateById(@RequestBody BaseUser baseUser) {
        baseUserService.updateById(baseUser);
        return R.success("成功.");
    }

    @GetMapping(value = "/getOne")
    public R<BaseUser> getOne(@RequestParam(value = "id") Integer id) {
        return R.success(baseUserService.getOneEntityById(id));
    }

    @PostMapping(value = "/remove")
    public R<Boolean> delete(@RequestParam(value = "id") Integer id) {
        return R.success(baseUserService.removeById(id));
    }

    @PostMapping(value = "/getList")
    public R<List<BaseUser>> getList(@RequestBody BaseUserPageDTO baseUserPageDTO) {
        IPage<BaseUser> pageRes = baseUserService.getPage(baseUserPageDTO.getBaseUser(), baseUserPageDTO.getPage());
        return R.success(pageRes.getRecords()).withPage(pageRes);
    }

    @PostMapping(value = "/batchUpdate")
    public R batchUpdate(@RequestBody List<Integer> ids) {
        return R.success(baseUserService.batchUpdate(ids));
    }

    @PostMapping(value = "/page")
    public R page(@RequestBody BaseUserPageDTO baseUserPageDTO) {
        IPage<BaseUser> baseUserList = baseUserService.getBaseUserList(baseUserPageDTO.getBaseUser(), baseUserPageDTO.getPage());
        return R.success(baseUserList.getRecords()).withPage(baseUserList);
    }

}
